package com.rrx.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rrx.model.Role;
import com.rrx.service.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class RoleController {
    @Resource
    RoleService roleService;

    @RequestMapping("/addRole")
    @ResponseBody
    public String addRole(HttpServletRequest request) {
        System.out.println(1);
        String[] permissions = request.getParameterValues("menu_permission");
        System.out.println(2);
        String name = request.getParameter("role_name");
        System.out.println(3);
        String remark = request.getParameter("role_remark");
        System.out.println(4);
        if(name != null && name.trim() != "") {
            if(roleService.addRole(name, remark)) {
                int id = roleService.getIdByName(name);
                System.out.println(5);
                if(id != -1) {
                    System.out.println(6);
                    if(permissions != null && permissions.length > 0) {
                        for (String s : permissions) {
                            System.out.println(s);
                            if (!roleService.addRoleAndPermission(id, s))
                                return "error";
                        }
                    }
                    return "success";
                }
            }
        }
        return "error";
    }

    @RequestMapping("/getRoleList")
    @ResponseBody
    public String getRoleList() {
        Role[] roles = roleService.getRoleList();
        ObjectMapper mapper = new ObjectMapper();
        try {
            String json = mapper.writeValueAsString(roles);
            return json;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "";
    }

    @RequestMapping("/getAdminRoles")
    @ResponseBody
    public String getAdminRoles(String adminId) {
        int id = -1;
        try {
            id = Integer.parseInt(adminId);
        }catch(Exception e) {return "idError";}
        if(id != -1) {
            Role[] roles = roleService.getAdminRole(id);
            ObjectMapper mapper = new ObjectMapper();
            try {
                String json = mapper.writeValueAsString(roles);
                return json;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return "error";
    }

    @RequestMapping("/updateRole")
    @ResponseBody
    public String updateRole(HttpServletRequest request) {
        String requestRoleId = request.getParameter("roleId");
        String requestName = request.getParameter("role_name").trim();
        if(requestName == "")
            return "nameError";
        String requestRemark = request.getParameter("role_remark").trim();
        String[] requestPermissions = request.getParameterValues("menu_permission");
        int id = -1;
        try {
            id = Integer.parseInt(requestRoleId);
        }catch(Exception e) {
            return "idError";
        }
        if(id != -1 && roleService.updateRole(id, requestName, requestRemark, requestPermissions))
            return "success";
        return "error";
    }

}
